﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using McsUtility;
using log4net;

namespace MinerManager.Forms
{
	public partial class MinerSyncLtcc : DevExpress.XtraEditors.XtraForm
	{
		ILog logger = LogManager.GetLogger(typeof(Program));
		private Common.AgentComponent Agent = null;

		public event EventHandler<EventArgs> OnSuccess;

		public MinerSyncLtcc(Common.AgentComponent agent)
		{
			InitializeComponent();
			this.Agent = agent;
		}

		#region 窗体事件

		private void MinerSyncLtcc_Load(object sender, EventArgs e)
		{
			//this.Agent.OnAddWalletReceive += new EventHandler<ByteEventArgs>(Agent_OnAddWalletReceive);
		}

		private void MinerSyncLtcc_FormClosing(object sender, FormClosingEventArgs e)
		{
			//this.Agent.OnAddWalletReceive -= new EventHandler<ByteEventArgs>(Agent_OnAddWalletReceive);
		}

		#endregion

		#region void Agent_OnAddWalletReceive(object sender, ByteEventArgs e)

		private void Agent_OnAddWalletReceive(object sender, ByteEventArgs e)
		{
			//McsUtility.Models.ResultModel result = ProtocolHelper.ReadPackageBody<McsUtility.Models.ResultModel>(e.Bytes);
		}

		#endregion

		#region void BtnSync_Click(object sender, EventArgs e)

		private void BtnSync_Click(object sender, EventArgs e)
		{
			string url = "http://www.ltcc.ren/api/webapi/syncwallet";
			string html = Utility.Post(url, "{UserName:\"" + ItemUserName.Text.Trim() + "\",PassWord:\"" + ItemPassword.Text + "\"}", Encoding.UTF8);
			if (!string.IsNullOrWhiteSpace(html))
			{
				//发送数据
				if (this.Agent.IsStarted)
				{
					try
					{
						Models.SyncWalletResult result = TomNet.Utility.Data.JsonHelper.FromJson<Models.SyncWalletResult>(html);

						foreach (Models.SyncWalletResultWallet info in result.Value)
						{
							int index = Common.Data.UserCurrency.FindIndex(p => p.Name.Equals(info.Code, StringComparison.CurrentCultureIgnoreCase));
							if (index >= 0)
							{
								foreach (string address in info.Addresses)
								{
									McsUtility.Models.WalletsModel sendInfo = new McsUtility.Models.WalletsModel
									{
										ID = Common.Data.UserCurrency[index].ID,
										Address = address,
										Remark = ""
									};
									byte[] bytes = ProtocolHelper.WritePackage<McsUtility.Models.WalletsModel>(ProtoType.MinerManagerAddWallets, sendInfo);
									this.Agent.AgentSend(bytes);
								}
							}
						}
					}
					catch { }

					OnSuccess?.Invoke(this, new EventArgs());
					this.Close();
				}
				else
				{
					MessageBox.Show("与服务器的链接断开，请重新登录系统。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
				}
			}
		}

		#endregion

		#region void BtnClose_Click(object sender, EventArgs e)

		private void BtnClose_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		#endregion



		#region void TextBox_KeyPress(object sender, KeyPressEventArgs e)

		private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == 13)
			{
				e.Handled = true;
				BtnSync_Click(BtnSync, new EventArgs());
			}

			if (e.KeyChar == 27)
			{
				BtnClose_Click(BtnClose, new EventArgs());
			}
		}

		#endregion
	}
}